﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

public partial class Controls_SeriesDropDownList : System.Web.UI.UserControl
{
	public enum EItem
	{
		DoNotAdd,
		ItemPleaseChoose,
		ItemAll,
		ItemPleaseChooseAndAll
	}

	private BrandManager m_BrandManager				= new BrandManager();
	private ProductSeriesManager m_SeriesManger		= new ProductSeriesManager();

	public delegate void SelectedSeriesChangedHandler(ProductSeries aSeries);
	public event SelectedSeriesChangedHandler SelectedSeriesChanged;

	public delegate void SelectedAllSeriesHandler();
	public event SelectedAllSeriesHandler SelectedAllSeries;

	public EItem AddListItem { get; set; }

	private Brand m_SelectedBrand;
	public Brand SelectedBrand
	{
		get
		{
			return this.m_SelectedBrand;
		}
		set
		{
			this.m_SelectedBrand			= value;
			this.BindSeries();
		}
	}

	public ProductSeries SelectedSeries
	{
		get
		{
			if (this.ddlSeries.Enabled == true && !String.IsNullOrEmpty(this.ddlSeries.SelectedValue) && !this.ddlSeries.SelectedValue.Equals("all"))
			{
				return this.m_SeriesManger.GetProductSeriesById(new Guid(this.ddlSeries.SelectedValue));
			}
			return null;
		}
		set
		{
			this.SelectedBrand				= value.Brand;
			this.ddlSeries.SelectedIndex	= this.ddlSeries.Items.IndexOf(this.ddlSeries.Items.FindByValue(value.ProductSeriesId.ToString()));
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (this.m_SelectedBrand != null)
		{
			this.BindSeries();
		}

		this.ddlSeries.AutoPostBack		= (null != this.SelectedSeriesChanged);
	}

	private void BindSeries()
	{
		this.ddlSeries.Items.Clear();
		List<ProductSeries> theSeriesList	= this.m_SeriesManger.GetProductSeriesByBrand(this.m_SelectedBrand);

		if (theSeriesList.Count > 0)
		{
			this.ddlSeries.DataSource		= theSeriesList;
			this.ddlSeries.DataTextField	= "Name";
			this.ddlSeries.DataValueField	= "ProductSeriesId";
			this.ddlSeries.DataBind();
			this.ddlSeries.Enabled	= true;

			switch (this.AddListItem)
			{
				case EItem.DoNotAdd:
					break;
				case EItem.ItemPleaseChoose:
					this.ddlSeries.Items.Insert(0, new ListItem("请选择...", string.Empty));
					break;
				case EItem.ItemAll:
					this.ddlSeries.Items.Insert(0, new ListItem("以下所有", "all"));
					break;
				case EItem.ItemPleaseChooseAndAll:
					this.ddlSeries.Items.Insert(0, new ListItem("请选择...", string.Empty));
					this.ddlSeries.Items.Insert(this.ddlSeries.Items.Count, new ListItem("以上所有", "all"));
					break;
			}
		}
		else
		{
			this.AddNoDataItemToDropDownList(ddlSeries);
		}
	}

	public void BrandChanged(Brand aBrand)
	{
		this.SelectedBrand	= aBrand;
	}

	private void AddNoDataItemToDropDownList(DropDownList aDropDownList)
	{
		aDropDownList.Items.Add(new ListItem("No Data", string.Empty));
		aDropDownList.SelectedIndex		= 0;
		aDropDownList.Enabled			= false;
	}

	protected void ddlSeries_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (String.IsNullOrEmpty(this.ddlSeries.SelectedValue))
		{
			return;
		}

		if (this.ddlSeries.SelectedValue.Equals("all"))
		{
			if (null != this.SelectedAllSeries)
			{
				this.SelectedAllSeries();
			}
			return;
		}

		if (null != this.SelectedSeriesChanged)
		{
			this.SelectedSeriesChanged(this.m_SeriesManger.GetProductSeriesById(new Guid(this.ddlSeries.SelectedValue)));
		}
	}
}
